<?php

namespace App\Services\SpiderContent;

use Carbon\Carbon;
use App\Models\User;
use App\Models\News;
use App\Jobs\RewardFirstParentJob;
use Illuminate\Support\Facades\Log;
use Rainsens\Dinglog\Facades\Dinglog;
use Illuminate\Database\Eloquent\Collection;
use Sunra\PhpSimple\HtmlDomParser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use JonnyW\PhantomJs\Client;
use Nesk\Puphpeteer\Puppeteer;


use DOMDocument;


class SpiderKeContent
{

    public function execute($baseUrl)
    {
    	ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

    	$puppeteer = new Puppeteer; // 新建 Puppeteer 实例

         $browser = $puppeteer->launch(['args'=>['--no-sandbox']]); // 启动无头浏览器
         $page = $browser->newPage(); // 打开新的标签页
       
         $page->tryCatch->goto($baseUrl,[
             'timeout' => 0,
             'read_timeout' => 30

         ]); // 访问页面
        	
       	 
         $html = $page->content();
       
        
        /* return $html; // 返回 js 渲染后的页面
        
		$html = Browsershot::url($baseUrl1)->setDelay(1000)->bodyHtml();*/
		
		

    	//$html = '<a href="aaaa"><div class="content-item"><div>测试</div></div></a>';
    	$dom = new DOMDocument();
    	

    	libxml_use_internal_errors(true);
    	//$html = htmlspecialchars($html);
    	
		
        $dom->loadHTML('<?xml encoding="UTF-8">' . $html);
		
      
      
        $elements = $dom->getElementsByTagName('div');

        

     	//$html = HtmlDomParser::str_get_html($html);

     	//dd($elements->nodeValue);

     	$data = [];
     	$i = 0;

     	$MinutsMatch = "/\d+分钟前/";
        foreach($elements as $key=>$val){
        	$url = "";

    		if (strpos($val->getAttribute('class'), 'kr-shadow-content') !== false) {
    			
    	 		$content = $dom->saveHTML($val);
    	 		
    	 		if(strstr($content,'分钟前')){
    	 			$matchTime = 0;
    	 			preg_match($MinutsMatch,$content,$match);
    	 			if(!empty($match)){
    	 				preg_match('/\d+/',$match[0],$times);
    	 				$matchTime = $times[0];
    	 			}

    	 			$data[$i]= ["images"=>"","brief"=>'',"content_cn"=>"","title_cn"=>"","images"=>"",'source'=>'36ke',"created_at"=>Carbon::now(),"url"=>"","started_at"=>Carbon::now()->subMinutes($matchTime)];


    	 			$contentDom = new DOMDocument();
    	 			$contentDom->loadHTML('<?xml encoding="UTF-8">' . $content);
	        		$aDom = $contentDom->getElementsByTagName('a');

    	 			

    	 			foreach($aDom as $childNode){
        			
        				if (strpos($childNode->getAttribute('class'), 'article-item-title') !== false) {
        					$data[$i]['title_cn'] = $childNode->textContent;
							$attrs= $childNode->attributes;
							foreach($attrs as $attr){
			    	 			if($attr->nodeName =="href"){

		    	 					$data[$i]['url'] = "https://36kr.com".$attr->textContent;
		    	 					
		    	 				}
			    	 			
			    	 		}
        				}

        			}

        			$imgDom = $contentDom->getElementsByTagName('img');

        			foreach($imgDom as $childNode){
        			
        				if (strpos($childNode->getAttribute('class'), 'scaleBig') !== false) {
        					
							$attrs= $childNode->attributes;
							foreach($attrs as $attr){
			    	 			if($attr->nodeName =="src"){

		    	 					$img = $attr->textContent;
		    	 					if($img){

				    					$imageContent = file_get_contents($img);

										$newFileName = "/images/".Str::random(40) . '.jpg'; // 假设是JPG格式
										Storage::disk('oss')->put($newFileName, $imageContent);
										
										// 你现在可以通过存储的路径访问图片
										$data[$i]['images'] = Storage::disk('oss')->path($newFileName);
										
				    				}
		    	 				}
			    	 			
			    	 		}
        				}

        			}


        			if(!empty($data[$i]['url'])){

						//dd($data[$i]['url']);

						 $page->tryCatch->goto($data[$i]['url'],[
				             'timeout' => 0,
				             'read_timeout' => 120,
				             'idle_timeout' => 120

				         ]); // 访问页面

				         $itemContent = $page->content();

						$AllcontentDom = new DOMDocument();


	     		   		$AllcontentDom->loadHTML('<?xml encoding="UTF-8">' . $itemContent);


	     		   		
	     		   		$divElements =  $AllcontentDom->getElementsByTagName('div');
	     		   		foreach($divElements as $divElement){
	     		   			

	     		   			if (strpos($divElement->getAttribute('class'), 'articleDetailContent') !== false) {
	     		   				$data[$i]['content_cn'] = $AllcontentDom->saveHTML($divElement);
	     		   				$data[$i]['content_cn'] = str_replace("_src","src",$data[$i]['content_cn']);
	     		   			}

	     		   			/*if (strpos($divElement->getAttribute('class'), 'thumbNum') !== false) {
	     		   				$data[$i]['likes'] = $AllcontentDom->saveHTML($divElement);
	     		   			}

	     		   			if (strpos($divElement->getAttribute('class'), 'favorite-count') !== false) {
	     		   				$data[$i]['collections'] = $AllcontentDom->saveHTML($divElement);
	     		   			}
	     		   			*/
	     		   		}

					}

					$i++;
	        		
    	 		}	
	        		
    		 }

        	
        	  
		}

		$browser->close();
		DB::table('deep_news')->insert($data);
		//DB::table('deep_news')->insert($data);
		//dd($data);
     	 
    	//dd($elements);

     	//dd(retrieve_url_contents($url));
     	//$html = file_get_contents('https://www.huxiu.com/club/1000.html?object_type=51&object_id=1');
     	//dd($html);
 
		
    }


    public function djNodes($nodes,$res)
    {
    	if($nodes->nextSibling && $nodes->nextSibling->nodeName!="h4"){

    		$res['content'] = $res['content'].$nodes->nextSibling->textContent;


    		if($nodes->nextSibling->childNodes){

    			foreach($nodes->nextSibling->childNodes as $nn){
    				if($nn->nodeName=="img"){
    					$res['image'] = $nn->getAttribute('src');
    				}
    			}
    		}
    		
    		if(isset($nodes->nextSibling->nextSibling)  && $nodes->nextSibling->nextSibling->nodeName != "h4"){

    			 $res = $this->djNodes($nodes->nextSibling,$res);
    		}
    	}

    	return  $res;
    	//dd($nodes->nextSibling->nextSibling);
    	

    }

	function getUrlSize($url) {
	    // 初始化cURL

	   ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

	    $ch = curl_init($url);
	     dd($ch);
	    // 设置cURL选项
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_HEADER, 1);
	    curl_setopt($ch, CURLOPT_NOBODY, 1);
	    
	    // 执行cURL请求
	    $output = curl_exec($ch);
	    // 获取内容长度
	    $size = 0;
	    if (preg_match('/Content-Length: (\d+)/', $output, $matches)) {

	        $size = (int)$matches[1];
	    }
	    
	    // 关闭cURL
	    curl_close($ch);
	    
	    return $size;
	}

	public function __invoke()
	{
		
		$baseUrl1 = "https://36kr.com/information/web_news/latest/";
		//Dinglog::text("========== refresh seckill product num start ......");

		//$today = date("Y-m-d");

		/*$client = Client::getInstance();
		 $request  = $client->getMessageFactory()->createRequest();
	     $response = $client->getMessageFactory()->createResponse();

	     $request->setMethod('GET');
	     $request->setUrl($baseUrl1);

		  // Send the request
		  $client->send($request, $response);

		  if($response->getStatus() === 200) {

		      // Dump the requested page content
		      dd( $response->getContent());
		  }

		  dd(123);*/
		

		$this->execute($baseUrl1);

		//Dinglog::text("========== refresh seckill product num end ......");
	}
}
